function createUser() {
	var sFirstName = $("#userCreateFirstName").val();
	var sLastName = $("#userCreateLastName").val();
	var sLogin = $("#userCreateLogin").val();
	var sPassword = $("#userCreatePassword").val();
	var sMail = $("#userCreateMail").val();
	var idEntity = $("#userCreateEntity").find(":selected").attr('id');
	var idRight = $("#userCreateRight").find(":selected").attr('id');
	var idStatus = $("#userCreateStatus").find(":selected").attr('id');
	if (sFirstName != null && sLastName != null && sLogin != null
			&& sPassword != null && sMail != null && idEntity != null
			&& idRight != null && idStatus != null & sFirstName != ''
			&& sLastName != '' && sLogin != '' && sPassword != ''
			&& sMail != '' && idEntity != '' && idRight != '' && idStatus != '') {
		alert(sFirstName);
		$.ajax({
			type : "GET",
			url : "createUser",
			dataType : "json",
			data : {
				firstName : sFirstName,
				lastName : sLastName,
				login : sLogin,
				password : sPassword,
				mail : sMail,
				idEntity : idEntity,
				idRight : idRight,
				idStatus : idStatus
			},
			success : function(returnValue) {

				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('User creation failed');
			}
		});
	} else {
		alert('Veuillez remplir tous les champs.');
	}
}

function createModule() {
	var sModuleName = $("#moduleCreateName").val();
	var sEnseirbId = $("#moduleCreateEnseirbId").val();
	var sDescription = $("#moduleCreateDescription").val();
	var sOwnerId = $("#moduleCreateIdOwner").val();

	if (sModuleName != null && sModuleName != '' && sEnseirbId != null
			&& sEnseirbId != '' && sDescription != null && sDescription != ''
			&& sOwnerId != null && sOwnerId != '') {
		$.ajax({
			type : "GET",
			url : "createModule",
			dataType : "json",
			data : {
				moduleName : sModuleName,
				enseirbId : sEnseirbId,
				description : sDescription,
				ownerId : sOwnerId
			},
			success : function(returnValue) {
				if (returnValue.result == true) {
					console.log(returnValue.result);
					clearFormInputs('moduleCreateForm');
					$("#moduleCreateSuccess").fadeIn();
					setTimeout(function() {
						$("#moduleCreateSuccess").fadeOut();
					}, 2000);
					var idUser = $("#loggedUserId").val();
					getUserModule(idUser);
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('Module creation failed');
			}
		});
	} else {
		alert('Veuillez remplir tous les champs.');
	}
}

function deleteModule(idModule) {
	if (confirm("Etes vous certain de vouloir supprimer ce module?")) {
		if (idModule != null) {
			$.ajax({
				type : "GET",
				url : "deleteModule",
				dataType : "json",
				data : {
					idModule : idModule
				},
				success : function(returnValue) {
					var idUser = $("#loggedUserId").val();
					getUserModule(idUser);
					goBackFromModuleCreate();
					return returnValue; // with return value excecute code
				},
				error : function(request, error) {
					alert('Fail to remove the module.');
				}
			});
		}
	}
}

function createRender() {
	var sRenderName = $("#renderCreateName").val();
	var idModule = $("#renderCreateModule").val();
	var sDescription = $("#renderCreateDescription").val();
	var sDeadlineLimit = $("#renderCreateDeadlineLimit").val();
	var sScale = $("#renderCreateScale").val();

	if (sRenderName != null && sRenderName != '' && idModule != null
			&& idModule != '' && sDescription != null && sDescription != ''
			&& sScale != null && sScale != '' && sDeadlineLimit != null
			&& sDeadlineLimit != '') {
		$.ajax({
			type : "GET",
			url : "createRender",
			dataType : "json",
			data : {
				renderName : sRenderName,
				idModule : idModule,
				description : sDescription,
				deadlineLimit : sDeadlineLimit,
				scale : sScale
			},
			success : function(returnValue) {
				if (returnValue.result == true) {
					console.log(returnValue.result);
					clearFormInputs('renderCreateForm');
					$("#renderCreateSuccess").fadeIn();
					setTimeout(function() {
						$("#renderCreateSuccess").fadeOut();
					}, 2000);
					var idUser = $("#loggedUserId").val();
					getUserRender(idUser);
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('Render creation failed');
			}
		});
	} else {
		alert('Veuillez remplir tous les champs.');
	}
}

function createGroup() {
	var sGroupName = $("#groupCreateName").val();
	var idCorrector = $("#groupCreateCorrector").val();
	var idRender = $("#groupCreateRender").val();
	var idNoteReport = $("#groupCreateNoteReport").val();

	if (sGroupName != null && sGroupName != '' && idCorrector != null
			&& idCorrector != '' && idRender != null && idRender != ''
			&& idNoteReport != null && idNoteReport != '') {
		$.ajax({
			type : "GET",
			url : "createGroup",
			dataType : "json",
			data : {
				groupeName : sGroupName,
				idCorrector : idCorrector,
				idRender : idRender,
				idNoteReport : idNoteReport
			},
			success : function(returnValue) {
				if (returnValue.result == true) {
					console.log(returnValue.result);
					clearFormInputs('groupCreateForm');
					$("#groupCreateSuccess").fadeIn();
					setTimeout(function() {
						$("#groupCreateSuccess").fadeOut();
					}, 2000);
					var idUser = $("#loggedUserId").val();
					getUserGroup(idUser);
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('Group creation failed');
			}
		});
	} else {
		alert('Veuillez remplir tous les champs.');
	}
}

function assignUserToGroup() {
	var idUser = $("#userInfoId").html();
	var idGroup = $("#groupIdToAssign").val();
	if (idUser != null && idUser != '' && idGroup != null && idGroup != '') {
		$.ajax({
			type : "GET",
			url : "assignUserToGroup",
			dataType : "json",
			data : {
				user : idUser,
				group : idGroup
			},
			success : function(returnValue) {
				if (returnValue != null && returnValue.result != null
						&& returnValue.result == true) {

				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('Fail to assign the user to the group.');
			}
		});
	}
}

function removeUserFromGroup(idGroup) {
	if (confirm("Voulez-vous supprimer l'utilisateur du groupe ?")) {
		var idUser = $("#userInfoId").html();
		if (idUser != null && idUser != '' && idGroup != null && idGroup != '') {
			$.ajax({
				type : "GET",
				url : "removeUserFromGroup",
				dataType : "json",
				data : {
					user : idUser,
					group : idGroup
				},
				success : function(returnValue) {
					if (returnValue != null && returnValue.result != null
							&& returnValue.result == true) {

					}
					return returnValue; // with return value excecute code
				},
				error : function(request, error) {
					alert('Fail to assign the user to the group.');
				}
			});
		}
	}
}
